const User = require('../model/User');
var express = require('express');
// 数据验证模块，
const validator = require('validator');
const Dbset = require('../model/db');
const Games = require('../model/Games');
const Reply = require('../model/Reply');
const Article = require('../model/Article')
const setting = require('../setting');
const saveMessage = require('../common/message');
var router = express.Router();
//论坛获取周期的user和点击的游戏
exports.luntan = (req, res, next) => {
    // console.log(req.session.user)
    if (req.session.user == null) {
        return res.json({ error: 1, message: '您好，登陆后才可以发布哟' })
    }
    var uid = req.session.user.username
    var gid = req.body.Gid.name
    console.log(uid)
    console.log(gid)
    Games.findOne({ "gameid": gid }, function (err, data) {
        if (err) {
            return res.json({ error: 2, message: '不存在了哟' })
        }
        console.log(data.gamename)
        res.json({
            error: 0, data: {
                uid: uid,
                gname: data.gamename,
                gimg: data.gameer_img,
            }
        })
    })
}
exports.wenzhang = (req, res, next) => {
    // console.log(req.body)
    // console.log(req.session.user._id)
    var logoing = req.session.user._id
    if (req.body.content == '') {
        return res.json({ error: 2, message: "内容不能为空" })
    }
    User.findById({ "_id": logoing }, (err, user) => {
        if (logoing == null) {
            return res.json({ error: 1, message: '要登录！登录！登！' })
        }
        let article = new Article()
        article.title = req.body.BiaoTi
        article.content = req.body.content
        article.create_time = new Date
        article.author = logoing
        article.game_id = req.body.gameid.name
        article.save().then((articles) => {
            res.json({ error: 0, message: '发表成功，去看看吧' })
            return articles
        }).then((articles) => {
            user.article_count += 1
            user.save()
            return articles
        }).catch((err) => {
            return res.end(err)
        })
    })
}
//获取当前游戏的文章
exports.focetext = (req, res, next) => {
    // console.log(req.body)
    var gid = req.body.gid.name
    Article.find({ "game_id": gid }).populate('author').then((data) => {
        // console.log(data)
        res.json({ error: 0, data: data })
    }).catch((err) => {
        res.json({ error: 1, message: "文章删除或者违法" })
    })
}
exports.lbig = (req, res, next) => {
    // console.log(req.body.arid.name)
    var article_id = req.body.arid.name
    // console.log(req.session.user._id)
    Article.findOne({ "_id": article_id }).populate('author').then((articles) => {
        // console.log(articles)
        res.json({ error: 0, data: articles })
    }).catch((err) => {
        res.json({ error: 1, message: "文章删除或者违法" })
    })
}
exports.shouc = (req, res, next) => {
    // console.log(req.body.article)
    var article_id = req.body.article
    var uid = req.session.user._id
    // console.log(uid)
    User.findById({ "_id": uid }, (err, user) => {
        // console.log(user)
        if (err) {
            return res.json({ error: 1, message: "用户为登录" })
        }
        if (user.article_counts.length <= 0) {
            user.article_counts.push(article_id)
            user.save()
            return res.json({ error: 0, message: '收藏成功' })
        } else {
            var boos = false
            user.article_counts.forEach(function (article) {
                if (article == article_id) {
                    return boos = true
                }
            });
            if (!boos) {
                user.article_counts.push(article_id)
                user.save()
            } else {
                return res.json({ error: 1, message: "收藏过了" })
            }
        }
    })
    // Article.findById({ "_id": article_id }).then((article) => {
    //     console.log(article)
    //     article.click_num += 1
    //     article.save().then((articles)=>{
    //     })
    // })
}
//文章回复
exports.article_reply = (req, res, next) => {
    // console.log(req.body)
    var conent = validator.trim(req.body.styleB)
    var article_id = req.body.article_id.name
    var article_author = req.body.article_author
    // console.log(conent)
    // console.log(article_id)
    // console.log(article_author)
    if (conent === '') {
        return res.json({ error: 1, message: "内容不能为空呢" })
    }
    Article.findById({ "_id": article_id }, (err, article) => {
        if (err) {
            return res.json({ error: 1, message: "文章被删除了呢" })
        }
        // console.log(article)
        // 123 zai 文章111 中  给你留言

        let reply = new Reply();
        reply.content = conent;
        reply.article_id = article_id;   // 文章的id  111
        reply.author = req.session.user._id  // 留言人123
        reply.bereply_id = article_author  //文章作者
        reply.save().then((reply) => {
            User.findById(req.session.user._id, (err, user) => {
                if (err) {
                    return res.end(err)
                }
                user.reply_count += 1;
                user.follows += 1
                user.save();
                req.session.user = user
            })
            return reply
        }).then((reply) => {
            article.comment_num += 1;
            article.click_num += 1
            article.last_reply_time = new Date();
            article.last_reply = reply._id;
            article.last_reply_author = req.session.user._id;
            article.save()
            return reply
        }).then((reply) => {
            if (article_author !== req.session.user._id) {
                saveMessage.saveReplyMessage(article_id, req.session.user._id, article_author, '');
            }
        }).then((reply) => {
            res.json({ error: 0, message: '评论好了呢' })
        }).catch((err) => {
            console.log(err)
        })
    })
}
//获取文章留言
exports.getarticle_reply = (req, res, next) => {
    // console.log(req.body)
    var article_id = req.body.article_id.name
    // console.log(article_id)
    Reply.find({ "article_id": article_id }).populate("author").populate(" bereply_id").then((data) => {
        // console.log(data)
        res.json({ error: 0, data: data })
    }).catch((err) => {
        console.log(err)
    })
}
//获取一级回复的id
exports.twicere = (req, res, next) => {
    // console.log(req.body)
    if (req.session.user._id == null) {
        return res.json({ error: 1, message: "用户为登录" })
    }
    var oncere = req.body.OnceRe
    Reply.findById({ "_id": oncere }).populate("author").populate(" bereply_id").then((data) => {
        res.json({ error: 0, data: data })
    }).catch((err) => {
        console.log(err)
    })
}
exports.TwiceReply = (req, res, next) => {
    var content = validator.trim(req.body.content)
    var onceid = req.body.once
    if (content === '') {
        return res.json({ error: 1, message: "内容不能为空呢" })
    }
    if (req.session.user._id == null) {
        return res.json({ error: 1, message: "用户为登录" })
    }
    Reply.findById({ "_id": onceid }, (err, reply) => {
        var yijiid = reply.author
        var article_id = reply.article_id
        reply.comment_num += 1
        reply.save()
        let replys = new Reply();
        replys.content = content;
        replys.create_time = new Date;
        replys.author = req.session.user._id;
        replys.bereply_id = yijiid;
        replys.reply_id = onceid;
        replys.save().then((replys) => {
            User.findById(req.session.user._id).then((user) => {
                user.reply_count += 1
                user.follows += 1
                user.save()
                req.session.user = user

            })
            return replys
        }).then((replys) => {
            Article.findById({ "_id": article_id }).then((article) => {
                console.log(article)
                article.comment_num += 1
                article.save()
            })
            return replys
        }).then((replys) => {
            if (req.session.user._id !== yijiid) {
                saveMessage.saveReplyMessage(article_id, '', req.session.user._id, yijiid);
            }

        }).then((reply) => {
            res.json({ error: 0, message: '意见收到了' })
        }).catch((err) => {
            console.log(err)
        })
    })
}

exports.gettwoReply = (req, res, next) => {
    // console.log(req.body)
    if (req.session.user._id == null) {
        return res.json({ error: 1, message: "用户为登录" })
    }
    var oncere = req.body.once
    Reply.find({ "reply_id": oncere }).populate("author").populate(" bereply_id").then((data) => {
        console.log(data)
        res.json({ error: 0, data: data })
    })
}
exports.dell = (req, res, next) => {
    console.log(req.body.id)
    Reply.findById(req.body.id, function (err, reply) {
        if (reply.author !== req.session.user._id) {
            return res.json({ error: 1, message: "言论自由，心底纯净" })
        } else {
            Reply.findByIdAndRemove(req.body.id, function (err) {
                res.json({ error: 0, message: "删了删了，别急" })
            })
        }
    })
}